app.controller('SaleModalCtrl', function ($scope, $filter, $modalInstance, toaster, param, ajax, fileUpload, cos) {

	$scope.dplist = [];
	$scope.rulelist = [];
	$scope.statuslist = [];
	$scope.timeRange = {};

	$scope.submitting = false;
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	$scope.ms = $scope.param.ms;
	$scope.dplist = [];
	
	if ($scope.param.type == "update") {
		var start = $filter("date")($scope.param.data.startTime, "yyyy-MM-dd HH:mm:ss");
		$scope.timeRange.startTime = start;
		var end = $filter("date")($scope.param.data.endTime, "yyyy-MM-dd HH:mm:ss");
		$scope.timeRange.endTime = end;
		$scope.flag = true;
	}

	//模块列表读取
	$scope.loaddplist = function () {
		for (var i = 0; i < $scope.ms.length; i++) {
			if ($scope.ms[i] == 1) {
				$scope.dplist.push({ id: 1, name: "订票" });
			} else if ($scope.ms[i] == 2) {
				$scope.dplist.push({ id: 2, name: "城际约租" });
			} else if ($scope.ms[i] == 3) {
				$scope.dplist.push({ id: 3, name: "跨城巴士" });
			}
		}

		if ($scope.param.data.moduleId != null) {
			for (i = 0; i < $scope.dplist.length; i++) {
				if ($scope.param.data.moduleId == 1 && $scope.dplist[i].name == "订票") {
					$scope.dplist.selected = $scope.dplist[i];
				} else if ($scope.param.data.moduleId == 2 && $scope.dplist[i].name == "城际约租") {
					$scope.dplist.selected = $scope.dplist[i];
				} else if ($scope.param.data.moduleId == 3 && $scope.dplist[i].name == "跨城巴士") {
					$scope.dplist.selected = $scope.dplist[i];
				}
			}
		} else {
			$scope.dplist.selected = $scope.dplist[0];
		}
	};

	$scope.loaddplist();

	//状态读取
	$scope.loadStatusList = function () {
		$scope.statuslist =
			[
				{ id: 1, name: "未激活" },
				{ id: 2, name: "已激活" },
				{ id: 3, name: "进行中" },
				{ id: 4, name: "结束" }
			];
		//设置默认值
		if ($scope.param.data.status != undefined) {
			var id = $scope.param.data.status;
			$scope.statuslist.selected = $scope.statuslist[id - 1];
		} else {
			$scope.statuslist.selected = $scope.statuslist[0];
		}
	};

	//促销规则列表读取
	$scope.loadRuleList = function () {
		var promise = ajax.call(new Req.GET({
			resource: 'sales/rule/list'
		}));
		promise.then(function (result) {
			$scope.rulelist = result.result;
			//设置默认值
			if ($scope.param.data.salesRuleId != undefined) {
				var id = $scope.param.data.salesRuleId;
				var sel = 0;
				for (var i = 0; i < $scope.rulelist.length; i++) {
					sel++;
					if ($scope.rulelist[i].id == id) {
						break;
					}
				}

				$scope.rulelist.selected = $scope.rulelist[sel - 1];
			} else {
				$scope.rulelist.selected = $scope.rulelist[0];
			}

		}, function () { });
	};

	$scope.loadRuleList();
	$scope.loadStatusList();

	$scope.ok = function () {
		var uploadUrl = cos.host() + cos.img() + "?attributeName=sales";
		if ($scope.param.type == "insert") {
			if ($scope.myForm.$valid) {
				if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
					if ($scope.timeRange.startTime > $scope.timeRange.endTime) {
						toaster.pop('warning', '错误', '开始日期不能大于结束日期!');
						return;
					} else {
						var starTime = $filter("date")($scope.timeRange.startTime, "yyyy/MM/dd HH:mm:ss");
						var date = new Date(starTime);
						$scope.param.data.startTime = date;

						var endTime = $filter("date")($scope.timeRange.endTime, "yyyy/MM/dd HH:mm:ss");
						var date1 = new Date(endTime);
						$scope.param.data.endTime = date1;
					}
				} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
					toaster.pop('warning', '错误', '请选择正确的时间');
					$scope.submitting = false;
					return;
				}
				if ($scope.fileToUpload != undefined) {
					$scope.param.data.moduleId = $scope.dplist.selected.id;
					$scope.param.data.status = $scope.statuslist.selected.id;
					$scope.param.data.salesRuleId = $scope.rulelist.selected.id;
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.imageUrl = result.filePath;
								$modalInstance.close($scope.param);
							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				} else {
					toaster.pop('warning', '提示', '请上传一张图片！');
					$scope.submitting = false;
					return;
				}
			}
		} else if ($scope.param.type == "update") {
				if ($scope.timeRange.startTime && $scope.timeRange.endTime) {
					if ($scope.timeRange.startTime > $scope.timeRange.endTime) {
						toaster.pop('warning', '错误', '开始日期不能大于结束日期!');
						return;
					} else if ($scope.param.data.subject == null || $scope.param.data.subject == "" || $scope.param.data.subject == undefined) {
						toaster.pop('warning', '错误', '主题不能为空');
						$scope.submitting = false;
						return;
					} else if ($scope.param.data.description == null || $scope.param.data.description == "" || $scope.param.data.description == undefined) {
						toaster.pop('warning', '错误', '说明不能为空');
						$scope.submitting = false;
						return;
					}
				} else if ($scope.timeRange.startTime == null || $scope.timeRange.endTime == null) {
					toaster.pop('warning', '错误', '请选择正确的时间');
					$scope.submitting = false;
					return;
				}
				$scope.param.data.moduleId = $scope.dplist.selected.id;
				$scope.param.data.status = $scope.statuslist.selected.id;
				$scope.param.data.salesRuleId = $scope.rulelist.selected.id;

				var s = $filter("date")($scope.timeRange.startTime, "yyyy/MM/dd HH:mm:ss");
				var st = new Date(s);
				var e = $filter("date")($scope.timeRange.endTime, "yyyy/MM/dd HH:mm:ss");
				var et = new Date(e);
				$scope.param.data.startTime = st;
				$scope.param.data.endTime = et;
				if ($scope.fileToUpload == undefined) {
					$modalInstance.close($scope.param);
				} else {
					fileUpload.upload($scope.fileToUpload, uploadUrl, null,
						function (result) {
							if (result.code == 200) {
								$scope.param.data.imageUrl = result.filePath;
								$modalInstance.close($scope.param);
							} else {
								toaster.pop('error', '错误', result.message);
							}
							$scope.uploading = false;
						}, function () {
							toaster.pop('error', '错误', '文件上传失败！');
							$scope.uploading = false;
						}
					);
				}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
	};
});



